<?php 
/**
 * Install Authority
 *
 */
	set_time_limit(0);
	ini_set("memory_limit","200M");

	include_once('../../config.php');
	require_once('../../classes/class.mysqli_database.php');
	require_once('../../classes/class.taxamatch.php');
	require_once('../../classes/class.nearmatch.php');
	require_once('../../classes/class.normalize.php');
class Install_Auth
{	
	//Constructor
	function __construct($authName) {
	this.$authName=$authName;
	}
		
	// Create Genus
	function buildGenus(){
		// database connection
		$db = select_source($table_name);
		// checks for csv file and creates tables with the postfix
		$postfix = '_' . $table_name;
		
		if(file_exists('../authorities/' . $filename)) {
			$data = fgetcsv($handle, 1000, ",");

			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)	{
				$master[$data[0]] = $data[2];
				$genus_id_desc = $data[$genus_id];
				$genus_desc    = $data[$genus];
				$authority_desc = $data[$authority];
				$genus_length = strlen($genus_desc);

				$norm = new Normalize();
				$nm = new NearMatch();
				$search_genus_name = $norm->normalize( $genus_desc );
				$near_match_genus = $nm->near_match( $genus_desc );
		
				$query = sprintf("INSERT INTO `genlist%s` (`GENUS_ID`, `GENUS`, `AUTHORITY`, `GEN_LENGTH`, `NEAR_MATCH_GENUS`, `SEARCH_GENUS_NAME`) VALUES  ('%s','%s','%s',%s,'%s','%s')"
						, mysql_escape_string($postfix)
						, mysql_escape_string($genus_id_desc)
						, mysql_escape_string($genus_desc)
						, mysql_escape_string($authority_desc)
						, mysql_escape_string($genus_length)
						, mysql_escape_string($near_match_genus)
						, mysql_escape_string($search_genus_name)
						);
				$db->query($query);
			}
		}
		return true;	
	}
	
	// Create Species
	function buildSpecies(){
		// database connection
		$db = select_source($table_name);
		// checks for csv file and creates tables with the postfix
		$postfix = '_' . $table_name;

		if(file_exists('../authorities/' . $filename)) {		
			$data = fgetcsv($handle, 1000, ",");
			
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
				$species_id_desc = $data[$species_id];
				$species_desc = $data[$species];
				$species_length = strlen($species_desc);
				$genus_id_desc = $data[1];
				$genus_desc = $master[$data[1]];
						
				$norm = new Normalize();
				$nm = new NearMatch();
				$search_species_name = $norm->normalize( $species_desc );
				$near_match_species = $nm->near_match( $species_desc );
	
				$authority_desc =  $data[$authority];
		
				$query = sprintf("INSERT INTO `splist%s` (`SPECIES_ID`, `GENUS_ORIG`, `SPECIES`, `GENUS_ID`, `AUTHORITY`, `SP_LENGTH`, `NEAR_MATCH_SPECIES`, `SEARCH_SPECIES_NAME`) VALUES ('%s','%s','%s','%s','%s',%s,'%s','%s') "
								, mysql_escape_string($postfix)
								, mysql_escape_string($species_id_desc)
								, mysql_escape_string($genus_desc)
								, mysql_escape_string($species_desc)
								, mysql_escape_string($genus_id_desc)
								, mysql_escape_string($authority_desc)
								, mysql_escape_string($species_length)
								, mysql_escape_string($near_match_species)
								, mysql_escape_string($search_species_name)
						);
				$db->query($query);	
			}
		}
		return true;
	}
	
	//Table Creation
	function createTables($postfix,$db) {
		$query = sprintf("CREATE TABLE IF NOT EXISTS `auth_abbrev%s` (
		`AUTH_ABBR` varchar(200) NOT NULL,
		`AUTH_FULL` varchar(200) NOT NULL,
	  UNIQUE KEY `AUTH_ABBR` (`AUTH_ABBR`,`AUTH_FULL`)		
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", mysql_escape_string($postfix));
		$db->query($query);
		
		$query = sprintf("CREATE TABLE IF NOT EXISTS `genlist%s` (
		`GENUS_ID` varchar(150) NOT NULL,
		`GENUS` varchar(50) default NULL,
		`AUTHORITY` varchar(150) default NULL,
		`GEN_LENGTH` int(2) default NULL,
		`NEAR_MATCH_GENUS` varchar(50) default NULL,
		`SEARCH_GENUS_NAME` varchar(50) default NULL,
		KEY `GENUS_ID` (`GENUS_ID`),
  	KEY `GEN_LENGTH` (`GEN_LENGTH`),
	  KEY `NEAR_MATCH_GENUS` (`NEAR_MATCH_GENUS`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", mysql_escape_string($postfix));
		$db->query($query);
		
		$query = sprintf("CREATE TABLE IF NOT EXISTS `genus_id_matches%s` (
		`genus_id` varchar(150) NOT NULL,
		`genus` varchar(50) default NULL,
		`genus_ed` int(11) default NULL,
		`phonetic_flag` char(1) default NULL
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", mysql_escape_string($postfix));
		$db->query($query);
		
		$query = sprintf("CREATE TABLE IF NOT EXISTS `species_id_matches%s` (
		`species_id` varchar(150) NOT NULL,
		`genus_species` varchar(50) default NULL,
		`genus_ed` int(11) default NULL,
		`species_ed` int(11) default NULL,
		`gen_sp_ed` int(11) default NULL,
		`phonetic_flag` char(1) default NULL
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", mysql_escape_string($postfix));
		$db->query($query);
		
		$query = sprintf("CREATE TABLE IF NOT EXISTS `splist%s` (
		`SPECIES_ID` varchar(150) NOT NULL,
		`GENUS_ORIG` varchar(50) default NULL,
		`SPECIES` varchar(60) default NULL,
		`GENUS_ID` varchar(150) default NULL,
		`AUTHORITY` varchar(150) default NULL,
		`SP_LENGTH` int(2) default NULL,
		`NEAR_MATCH_SPECIES` varchar(60) default NULL,
		`SEARCH_SPECIES_NAME` varchar(60) default NULL,
		 KEY `GENUS_ID` (`GENUS_ID`),
		 KEY `SP_LENGTH` (`SP_LENGTH`),
  	 KEY `NEAR_MATCH_SPECIES` (`NEAR_MATCH_SPECIES`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", mysql_escape_string($postfix));
		$db->query($query);
		
		return true;
	}
	//var ia = new InstallAuth($authName);
	//ia->creatTable();                                      
	//ia->buildAuthors( $send something from your dwca );
	//ia->buildGenus();
	//ia->buildSpecies();
}
?>